/**
 * @program: LeetCode
 * @description: LeetCode : 337. 打家劫舍 III
 * @author: WXY
 * @create: 2023-01-28 10:19
 * @Version 1.0
 **/
public class Num337_rob {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode() {
        }
    }

    public static class Info{
        int yes;
        int no;

        public Info(int yes, int no) {
            this.yes = yes;
            this.no = no;
        }
    }

    public static int rob(TreeNode root) {
        Info ans = process(root);
        return Math.max(ans.no, ans.yes);
    }

    private static Info process(TreeNode root) {
        if (root == null) {
            return new Info(0, 0);
        }
        Info leftInfo = process(root.left);
        Info rightInfo = process(root.right);
        int yes = root.val;
        int no = 0;
        yes += leftInfo.no + rightInfo.no;
//        int no1 = leftInfo.no + rightInfo.no;
//        int no2 = leftInfo.no + rightInfo.yes;
//        int no3 = leftInfo.yes + rightInfo.no;
//        int no4 = leftInfo.yes + rightInfo.yes;
//        no += Math.max(Math.max(Math.max(no1, no2), no3), no4);
        no += Math.max(leftInfo.no, leftInfo.yes) + Math.max(rightInfo.no, rightInfo.yes);
        return new Info(yes, no);
    }

    public static void main(String[] args) {
        TreeNode head = new TreeNode(3);
        head.left = new TreeNode(4);
        head.right = new TreeNode(5);
        head.left.right = new TreeNode(3);
        head.left.left = new TreeNode(1);
        head.right.right = new TreeNode(1);
        int rob = rob(head);
        System.out.println(rob);
    }
}
